/* 
 * Copyright 2012 Andrew Brock
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.catchwa.myvideobrowser;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.swing.SwingWorker;
import org.catchwa.myvideobrowser.api.TheMovieDbApi;
import org.catchwa.myvideobrowser.model.Movie;

/**
 *
 * @author Andrew
 */
public class MoviePopulator extends SwingWorker<List<Movie>, Movie> {

  @Override
  protected List<Movie> doInBackground() throws Exception {
    MovieScanner.Result results = MovieScanner.scan(new File(Main.getProperties().getString("root.directory")), null);

    List<File> moviesToQuery = results.getFilesToSubmit();
    TheMovieDbApi tmdb = new TheMovieDbApi();

    List<Movie> movies = new ArrayList<Movie>();
    for (File f : moviesToQuery) {
      Movie m = tmdb.getMovieFromFile(f, null);
      if (m != null) {
        Main.getDatabase().add(m);
        movies.add(m);
        publish(m);
      }
    }
    return movies;
  }

  @Override
  protected void process(List<Movie> movies) {
    for (Movie m : movies) {
      Main.getBrowser().addMovieToTable(m);
    }
  }
}
